$().ready(function() {
    getUserList();
});

function getUserList(){
    $.ajax({
        cache: true,
        type: "GET",
        url: "/user/findAll",
        data:{},
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (res) {
            var str = "<option value=''>请选择</option>";
            if (res.code == 0) {
                for(var i=0; i<res.data.length; i++){
                    str += "<option value='"+ res.data[i].id +"'>"+ res.data[i].username +"</option>";
                }
                $("#friendId").append(str);
            } else {
                parent.layer.alert("加载用户列表失败！")
            }

        }
    });
}

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/friend/save",
        data: {
            "selfId": $("#selfId").val(),
            "friendId": $("#friendId").val()
        },
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (res) {
            if (res.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.alert(res.msg);
                $("#submit").attr("disabled",false);
            }

        }
    });
}
